<extend name="Public:template" />
<block name="page_header">
<include file="Public:header"/>
<!-- 购物车页面  -->
<ul class="header-sub clearfix ">
	<li class="previous"><a href="{:U('Index/index')}" class="btn btn-default  active" role="button"><span class="icon icon-search"></span>选购</a></li>
	<li ><h3>我的购物车</h3></li>
	<li class="next "><a href="{:U('CustOrder/showConfirm')}" class="btn btn-warning active " role="button">选择配送地址<span class="icon-white icon-chevron-right"></span></a></li>
</ul>
</block>
<block name="page_body">

<div class="content clearfix">
	<div class="productList" id="productListView">
		<volist name="cartItems" id="val">
		<dl id="{$val.prod_id}">
			<dt class="pull-left">
				<a href="#" onclick="return false;">
					<img class="lazy"  src="{$val.prod_id|m_prod_pic}"/>
				</a>
			</dt>
			<dd>
				
				<h5><a href="#"  onclick="return false;">{$val.name} {$val.spec|m_prod_spec=$val['spec_qty'],$val['spec_unit']}</a></h5>
				<p class="promTags"><span class="label label-danger">{$val.stock_message}</span></p>
				<p class="price">
					<span class="now" id="itemPriceReal{$val.prod_id}" price_real="{$val.price_real}">¥{$val.price_real}</span>
					<span class="old" id="itemPriceSale{$val.prod_id}" price_sale="{$val.price_sale}">¥{$val.price_sale}</span>
				</p>
				
				<div class="cartGroupBtn">
					<div class="input-group" id="cartAddBtn{$val.prod_id}">
						<span class="input-group-btn">
							<a href="javascript:addToCart('{$val.prod_id}','-1','1');"  class="btn btn-default active input-sm " role="button" style="border-radius:0;" >-</a>
						</span>
						<input type="number" class="form-control input-sm" style="padding:5px 0;text-align:center;border:1px solid #cccccc;" id="itemNum{$val.prod_id}" name="itemNum[]" value="{$val.num}" onChange="addToCart('{$val.prod_id}',this.value,'2')"/>
						<span class="input-group-btn">
							<a href="javascript:addToCart('{$val.prod_id}','1','1');"  class="btn btn-default active input-sm " role="button" style="border-radius:0;" >+</a>
						</span>
					</div>
					<span class="cartLoadingBtn"  id="cartLoadingBtn{$val.prod_id}" style="display:none">
						<img  src="__ROOT__/statics/mobile/images/ajax_loading.gif">
					</span>
				</div>
			</dd>
		</dl>
		</volist>
	</div>

	<!--统计信息-->
	<div class="cartTotal mt5">
		<p class="text-right mt5 price" id="suminfo">
			您购买了<span class="text-danger" id="total_item" >{$cartPrice.total_item}</span>种商品，总共<span class="text-danger"  id="total_num">{$cartPrice.total_num}</span>件，总价<span class="text-danger" id="total_amt">¥{$cartPrice.total_amt|number_format=###, 2,'.',''}</span>
    	</p>
    	<p class="text-right mt5">
    		(订单金额超过20元免运费)
    	</p>
	</div>
	<if condition="$cartPrice.total_amt gt 0">
		<!--操作按钮-->
		<div class="row clearfix  mt15">
			<div class="col-xs-6 col-sm-6" >
				<a href="#" class="btn btn-default active  pull-left" role="button" onClick="doEmptyCart()"><span class="icon icon-trash"></span>清空购物车</a>
			</div>
			<div class="col-xs-6 col-sm-6" >
				<a href="{:U('CustOrder/showConfirm')}" class="btn btn-warning active  pull-right" role="button">选择配送地址<span class="icon-white icon-chevron-right"></span></a>
			</div>
		</div>
	</if>

</div>
</block>
<block name="script_footer">
<script type="text/javascript">
function addToCart(prod_id,num,edittype){
	//document.location.href = "/cart/doadd.html?prod_id="+prod_id+"&num="+num+"&edittype="+edittype;
	var currNum = parseInt( $("#itemNum"+prod_id).val());
	var leftNum = currNum+parseInt(num);
	if(currNum<=0||(edittype=='1' && leftNum<=0)){
		if(!confirm('确定将该商品从购物车中移除？')){
			return;
		}
	}
	$("#cartLoadingBtn"+prod_id).show();
	$("#cartAddBtn"+prod_id).hide();
	$.ajax({
		'async': true, 
        'url': "/cart/doadd.html",
        'dataType': "json",
        'data': {'prod_id':prod_id,'num':num,'edittype':edittype},
        'success': function (data) {
        	if(data['error']=='0'){
	        	if(data['message']!=''){
	        		alert(data['message']);
	        	}
	        	var cart_num = data['cart_num'];
	        	var item_num = data['item_num'];
	        	$("#cartNum").html(cart_num);
	        	$("#itemNum"+prod_id).val(item_num);
	        	if(item_num<=0){
	        		$("#"+prod_id).remove();
	        	}
	        	if(cart_num<=0){
	        		$(".cartAction").hide();
	        	}
	        	calcPrice();
        	}else{
        		alert(data['message']);
        	}
        	$("#cartLoadingBtn"+prod_id).hide();
        	$("#cartAddBtn"+prod_id).show();
        }
    });
}
function calcPrice(){
	var totalItem = 0;
	var totalAmt = 0;
	var totalNum = 0;
	$("#productListView dl").each(function(){
		var prod_id = $(this).attr("id");
		var item_num = parseInt($("#itemNum"+prod_id).val());
		var item_price = parseFloat($("#itemPriceReal"+prod_id).attr("price_real"));
		totalAmt += item_price*item_num;
		totalNum += item_num;
		totalItem++;
	});
	$("#total_item").html(totalItem);
	$("#total_num").html(totalNum);
	$("#total_amt").html("¥"+totalAmt.toFixed(2));
	
}
function doEmptyCart(){
	if(confirm('确定清空购物车?')){
		document.location.href="/Cart/doEmpty.html";
	}
}
</script>
</block>